<template>
	<view class="pageBg">
		<Skeleton v-if="skeletonLoading" type="newsList"></Skeleton>
		<u-empty image="search" description="没有找到对应的数据" style="background-color:#fff" v-if="show_empty === true" />
		<view class="list_wrapper">
			<view @click="jumpTo(item)" v-for="(item, index) in dataset" :key="index" class="list_block">
				<view class="title substring2">
					{{ item.title }}
				</view>

				<view class="other">
					<view class="time">{{ item.addtime | timeFilter }}</view>
					<view class="num">{{ item.click }}</view>
				</view>

			</view>
			<u-loadmore class="u-loadmore" v-if="dataset.length>=pagesize" :status="finished_status" />
		</view>
	</view>
</template>

<script>
	import {
		parseTime
	} from '@/utils/index'
	import http from '@/utils/http'
	import api from '@/api'
	import Skeleton from '@/components/Skeleton.vue'
	export default {
		name: 'Notice',
		filters: {
			timeFilter(timestamp) {
				return parseTime(timestamp, '{y}-{m}-{d} {h}:{i}')
			}
		},
		components: {
			Skeleton
		},
		data() {
			return {
				skeletonLoading: true,
				finished_status: 'loadmore',
				dataset: [],
				show_empty: false,
				page: 1,
				pagesize: 15,
				linkUrl: ''
			}
		},
		watch: {
			$Route(to, from) {
				// 对路由变化作出响应...
				if (from.name != 'noticeShow' && to.name == 'noticeList') {
					this.fetchData(true)
				}
			}
		},
		beforeRouteLeave(to, from, next) {
			if (to.name == 'noticeShow') {
				if (!from.meta.keepAlive) {
					from.meta.keepAlive = true
				}
				next()
			} else {
				from.meta.keepAlive = false
				next()
			}
		},
		created() {
			this.fetchData(true)
		},
		onReachBottom() {
			this.page++
			this.fetchData(false)
		},
		methods: {
			fetchData(init) {
				this.show_empty = false
				if (init === true) {
					this.page = 1
					this.finished_status = 'nomore'
				}
				let conditions = {
					page: this.page,
					pagesize: this.pagesize
				}

				http
					.get(api.notice_list, conditions)
					.then(res => {
						if (init === true) {
							this.dataset = [...res.data.data.items]
						} else {
							this.dataset = this.dataset.concat(res.data.data.items)
						}
						this.skeletonLoading = false
						// 数据全部加载完成
						if (res.data.data.items.length < this.pagesize) {
							this.finished_status = 'nomore'
							if (init === false) {
								this.finished_status = 'nomore'
							} else if (res.data.data.items.length == 0) {
								this.show_empty = true
							}
						}
					})
					.catch(() => {})
			},
			jumpTo(item) {
				if (item.link_url == '') {
					uni.navigateTo({
						url: '/commonViews/NoticeShow?id=' + item.id
					})
					this.linkUrl = ''
				} else {
					this.linkUrl = item.link_url
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list_wrapper {
		padding: 30rpx 30rpx 0;

		.list_block {
			background-color: #ffffff;
			border-radius: 14rpx;
			margin-bottom: 18rpx;
			padding: 28rpx;

			.title {
				line-height: 47rpx;
				word-break: break-all;
				font-size: 32rpx;
				color: #2A2A2A;
				margin-bottom: 10rpx;
				font-weight: 500;
			}

			.other {
				display: flex;
				color: #999999;
				font-size: 26rpx;
				padding: 6rpx 0;

				.time {
					padding-left: 34rpx;
					margin-right: 47rpx;
					background: url('../static/img/time.png') no-repeat left center / 21rpx 20rpx;
				}

				.num {
					padding-left: 34rpx;
					background: url('../static/img/num.png') no-repeat left center / 20rpx 16rpx;
				}
			}

		}
	}
</style>
